import request from '@/sheep/request';

const CommunityUtil = {
  // 发布动态
  publishCommunity: (params) => {
    return request({
      url: '/member/community/publish',
      method: 'POST',
      data: params,
      custom: {
        auth: true,
        showLoading: true,
        showSuccess: true,
        successMsg: '发布成功',
      },
    });
  },
  
  // 获取动态列表
  getCommunityList: (params) => {
    return request({
      url: '/member/community/page',
      method: 'GET',
      params: {
        userId: params.userId,
        page: params.page || params.pageNo || 1,
        pageSize: params.pageSize || 10,
      },
      custom: {
        showLoading: false, // 不显示加载动画
        showError: true,    // 显示错误信息
        auth: false,        // 不需要授权
      },
    });
  },
  
  // 获取我的朋友圈动态列表
  getMyPage: (params) => {
    return request({
      url: '/member/community/my-page',
      method: 'GET',
      params: {
        page: params.page || params.pageNo || 1,
        pageSize: params.pageSize || 10,
      },
      custom: {
        showLoading: false, // 不显示加载动画
        showError: true,    // 显示错误信息
        auth: true,        // 需要授权
      },
    });
  },
  // 根据userId获取朋友圈动态列表
  getUserPage: (params) => {
    return request({
      url: '/member/community/user-page',
      method: 'GET',
      params: {
        userId: params.userId,
        page: params.page || params.pageNo || 1,
        pageSize: params.pageSize || 10,
      },
      custom: {
        showLoading: false, // 不显示加载动画
        showError: true,    // 显示错误信息
        auth: false,        // 不需要授权
      },
    });
  },
  // 获取动态详情
  getCommunityDetail: (params) => {
    return request({
      url: '/member/community/get',
      method: 'GET',
      params: {
        id: params.id
      },
      custom: {
        showLoading: false, // 不显示加载动画
        showError: true,    // 显示错误信息
        auth: false,        // 不需要授权
      },
    });
  },
  
  // 添加动态点赞
  createLike: (params) => {
    // 提取ID值
    let communityId;
    if (typeof params === 'object' && params !== null && 'id' in params) {
      communityId = params.id;
    } else {
      communityId = params;
    }
    return request({
      url: '/member/community-like/create',
      method: 'POST',
      data: {
        communityId: communityId,
      },
      custom: {
        auth: true,
        showSuccess: true,
        successMsg: '点赞成功',
      },
    });
  },
  
  // 取消动态点赞
  cancelLike: (params) => {
    // 提取ID值
    let communityId;
    if (typeof params === 'object' && params !== null && 'id' in params) {
      communityId = params.id;
    } else {
      communityId = params;
    }
    return request({
      url: '/member/community-like/cancel',
      method: 'DELETE',
      data: {
        communityId: communityId,
      },
      custom: {
        auth: true,
        showSuccess: true,
        successMsg: '取消点赞',
      },
    });
  },
  
  // 检查是否已点赞动态
  isLiked: (id) => {
    return request({
      url: '/member/community-like/check',
      method: 'GET',
      params: {
        id: id
      },
      custom: {
        auth: true,
        showLoading: false,
      },
    });
  },
  
  // 添加动态收藏
  createFavorite: (params) => {
    // 提取ID值
    let communityId;
    if (typeof params === 'object' && params !== null && 'id' in params) {
      communityId = params.id;
    } else {
      communityId = params;
    }
    return request({
      url: '/member/community-favorite/create',
      method: 'POST',
      data: {
        communityId: communityId,
      },
      custom: {
        auth: true,
        showSuccess: true,
        successMsg: '收藏成功',
      },
    });
  },
  
  // 取消动态收藏
  cancelFavorite: (params) => {
    // 提取ID值
    let communityId;
    if (typeof params === 'object' && params !== null && 'id' in params) {
      communityId = params.id;
    } else {
      communityId = params;
    }
    return request({
      url: '/member/community-favorite/cancel',
      method: 'DELETE',
      data: {
        communityId: communityId,
      },
      custom: {
        auth: true,
        showSuccess: true,
        successMsg: '取消收藏',
      },
    });
  },
  
  // 检查是否已收藏动态
    isFavorite: (item) => {
    return request({
      url: '/app-api/member/community-favorite/check',
      method: 'GET',
      params: {
        communityId: item.id
      }
    });
  },
  
  // 获取动态评论列表
  getCommentList: (params) => {
    return request({
      url: '/member/community-comment/list-by-community',
      method: 'GET',
      data: params,
      custom: {
        showLoading: false,
        showError: true,
        auth: false,
      },
    });
  },
  
  // 创建评论
  createComment: (params) => {
    return request({
      url: '/member/community-comment/create',
      method: 'POST',
      data: params,
      custom: {
        auth: true,
        showLoading: true,
        showSuccess: true,
        successMsg: '评论成功',
      },
    });
  },
  
  // 删除评论
  deleteComment: (params) => {
    return request({
      url: '/member/community-comment/delete',
      method: 'DELETE',
      data: params,
      custom: {
        auth: true,
        showLoading: true,
        showSuccess: true,
        successMsg: '删除成功',
      },
    });
  },
  
  // 删除动态
  deleteCommunity: (params) => {
    // 提取ID值
    let communityId;
    if (typeof params === 'object' && params !== null && 'id' in params) {
      communityId = params.id;
    } else {
      communityId = params;
    }
    return request({
      url: '/member/community/delete',
      method: 'DELETE',
      params: {
        id: communityId,
      },
      custom: {
        auth: true,
        showLoading: true,
        showSuccess: true,
        successMsg: '删除成功',
      },
    });
  },
  
  // 编辑动态
  updateCommunity: (params) => {
    return request({
      url: '/member/community/update',
      method: 'PUT',
      data: params,
      custom: {
        auth: true,
        showLoading: true,
        showSuccess: true,
        successMsg: '更新成功',
      },
    });
  },
};

export default CommunityUtil;